﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using MinecraftGPS.Data;

namespace MinecraftGPS
{
    public partial class MainForm : Form
    {
        private Timer timer;
        private IList<Bookmark> bookmarks;
        private Coordinates relativePos;

        public MainForm()
        {
            InitializeComponent();

            this.Text += " " + this.GetType().Assembly.GetName().Version.ToString();

            CurrentWorld = null;

            timer = new Timer();
            timer.Interval = 500;
            timer.Tick += Timer_Tick;
            timer.Start();
        }

        protected override void OnClosed(EventArgs e)
        {
            CurrentWorld = null;
            base.OnClosed(e);
        }

        private World currentWorld;
        private World CurrentWorld
        {
            get { return currentWorld; }
            set
            {
                if (currentWorld == value)
                    return;

                if (currentWorld != null)
                    Bookmark.Save(currentWorld, bookmarks);

                currentWorld = value;

                add.Enabled = currentWorld != null;
                if (currentWorld != null)
                    bookmarks = Bookmark.Load(currentWorld);
                else
                    bookmarks = null;

                bookmarkBindingSource.DataSource = bookmarks;

                bookmarksList.SelectedIndex = -1;
            }
        }

        void Timer_Tick(object sender, EventArgs e)
        {
            if (CurrentWorld == null)
            {
                toolStripStatusLabel1.Text = "No World";

                this.posX.Text = "";
                this.posY.Text = "";
                this.posZ.Text = "";

                this.chunkX.Text = "";
                this.chunkZ.Text = "";

                this.relativeX.Text = "";
                this.relativeY.Text = "";
                this.relativeZ.Text = "";

                timer.Stop();
            }
            else
            {
                Coordinates pos;

                try
                {
                    pos = CurrentWorld.GetPlayerPosition();
                }
                catch
                {
                    pos = new Coordinates();
                }

                if (pos.IsEmpty)
                {
                    toolStripStatusLabel1.Text = "Bad Data";

                    this.posX.Text = "";
                    this.posY.Text = "";
                    this.posZ.Text = "";

                    this.chunkX.Text = "";
                    this.chunkZ.Text = "";

                    this.relativeX.Text = "";
                    this.relativeY.Text = "";
                    this.relativeZ.Text = "";

                    timer.Stop();
                    add.Enabled = false;
                }
                else
                {
                    var worldPathBits = currentWorld.WorldPath.Split(Path.DirectorySeparatorChar);

                    toolStripStatusLabel1.Text = worldPathBits[worldPathBits.Length - 1];

                    this.posX.Text = pos.X.ToString();
                    this.posY.Text = pos.Y.ToString();
                    this.posZ.Text = pos.Z.ToString();

                    var chunk = pos.ToChunkCoordinates();

                    this.chunkX.Text = chunk.X.ToString();
                    this.chunkZ.Text = chunk.Z.ToString();

                    if (!relativePos.IsEmpty)
                    {
                        this.relativeX.Text = (pos.X - relativePos.X).ToString();
                        this.relativeY.Text = (pos.Y - relativePos.Y).ToString();
                        this.relativeZ.Text = (pos.Z - relativePos.Z).ToString();
                    }
                    else
                    {
                        this.relativeX.Text = "";
                        this.relativeY.Text = "";
                        this.relativeZ.Text = "";
                    }
                }
            }
        }

        private void CompactToolStripMenuItem_Click(object sender, EventArgs e)
        {
            standardToolStripMenuItem.Checked = false;
            compactToolStripMenuItem.Checked = true;

            this.Size = new Size(192, 182);

            bookmarksList.Visible = false;
            add.Visible = false;
            delete.Visible = false;
            edit.Visible = false;
            groupBox3.Visible = false;
        }

        private void StandardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            standardToolStripMenuItem.Checked = true;
            compactToolStripMenuItem.Checked = false;

            this.Size = new Size(280, 280);

            bookmarksList.Visible = true;
            add.Visible = true;
            delete.Visible = true;
            edit.Visible = true;
            groupBox3.Visible = true;
        }

        private void WorldToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (sender == world1ToolStripMenuItem)
                CurrentWorld = World.LoadSave(1);
            if (sender == world2ToolStripMenuItem)
                CurrentWorld = World.LoadSave(2);
            if (sender == world3ToolStripMenuItem)
                CurrentWorld = World.LoadSave(3);
            if (sender == world4ToolStripMenuItem)
                CurrentWorld = World.LoadSave(4);
            if (sender == world5ToolStripMenuItem)
                CurrentWorld = World.LoadSave(5);

            timer.Start();
        }

        private void Bookmarks_SelectedIndexChanged(object sender, EventArgs e)
        {
            delete.Enabled = bookmarksList.SelectedIndex != -1;
            edit.Enabled = bookmarksList.SelectedIndex != -1;

            if (bookmarksList.SelectedIndex != -1)
            {
                var bookmark = bookmarks[bookmarksList.SelectedIndex];
                relativePos = bookmark.Position;
            }
            else
                relativePos = new Coordinates();
        }

        private void Add_Click(object sender, EventArgs e)
        {
            BookmarkForm bookmarkForm = new BookmarkForm();
            bookmarkForm.BookmarkName = "New Bookmark";

            if (bookmarkForm.ShowDialog(this) == DialogResult.OK)
            {
                int x = Convert.ToInt32(posX.Text);
                int y = Convert.ToInt32(posY.Text);
                int z = Convert.ToInt32(posZ.Text);

                bookmarkBindingSource.Add(new Bookmark(bookmarkForm.BookmarkName, new Coordinates(x, y, z)));

                Bookmark.Save(currentWorld, bookmarks);

                bookmarksList.SelectedIndex = -1;
            }
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            bookmarkBindingSource.RemoveAt(bookmarksList.SelectedIndex);

            Bookmark.Save(currentWorld, bookmarks);
        }

        private void Edit_Click(object sender, EventArgs e)
        {
            BookmarkForm bookmarkForm = new BookmarkForm();
            bookmarkForm.BookmarkName = bookmarks[bookmarksList.SelectedIndex].Name;

            if (bookmarkForm.ShowDialog(this) == DialogResult.OK)
            {
                bookmarks[bookmarksList.SelectedIndex].Name = bookmarkForm.BookmarkName;

                Bookmark.Save(currentWorld, bookmarks);

                bookmarkBindingSource.ResetBindings(false);
            }
        }
    }
}
